﻿



const G = {};


G.closeLayer = function (index) {
    window.top. layer.close(index);
}
/**
 * 提示框
 * @param {提示信息} msg 
 * @returns 
 */
G.alert = function (msg) {
    return window.top.layer.alert(msg, {
        skin: 'layui-layer-molv'
    });
}
/**
 * load 框
 * @returns 
 */
G.loading = function (msg) {
    if (!msg) msg = "正在加载...";
    return window.top.layer.load(3, { content: msg });
}
/**
 * toast 框
 * @param {提示信息} msg 
 * @returns 
 */
G.toast = function (msg) {
    return window.top.layer.msg(msg);
}
G.msg = function (msg) {
    return window.top.layer.msg(msg);
}
G.tip = function (msg, elementId) {
    if (elementId && elementId[0] != "#") elementId = "#" + elementId;
    return window.top.layer.tips(msg, elementId);
}
G.tips = function (msg, elementId, options) {
    if (elementId && elementId[0] != "#") elementId = "#" + elementId;
    if (!options) G.tip(msg, elementId);
    return window.top.layer.tips(msg, elementId, options);
}
/**
 * 询问框
 * @param {提示信息} msg 
 * @param {回调函数} callback 
 * @returns 
 */
G.confirm = function (msg, callback) {
    return window.top.layer.confirm(msg,
        { icon: 3, title: '提示', skin: 'layui-layer-molv' },
        function (index) {
            if (callback) callback();
            window.top.layer.close(index);
        });
}

/**
 * 
 * @param {网页地址} url 
 * @param {弹层标题} title 
 * @param {弹层宽} width 
 * @param {弹层高} height 
 * @param {是否点击遮罩关闭} shadecolse 
 * @returns 
 */
G.openDlg = function (url, title, width, height, shadecolse = false) {
    return window.top.layer.open({
        type: 2,
        content: url,
        title: title,
        shade: [0.5, '#000'],
        shadeClose: shadecolse,
        maxmin: true,
        skin:'layui-layer-molv',
        area: [width + 'vw', height + 'vh'],

    });
}

/**
 * 可以设置按钮的弹层
 * @param {路由} url
 * @param {弹层标题} title
 * @param {弹层宽} width
 * @param {弹层高} height
 * @param {是否点击遮罩关闭} shadecolse
 * @param {按钮数组} btn
 */

G.openDlgFunc = function (url, title, width, height, shadecolse = false, btns = [{ btn: "确定", hander: () => { } }]) {

    let btnTxt = [];
    let handers = [];
    for (let i = 0; i < btns.length; i++) {
        btnTxt.push(btns[i].btn);
        handers.push(btns[i].hander);
    }

    let options = {
        type: 2,
        content: url,
        title: [title || '新建', 'background-color: #2e8ded'],
        shade: [0.5, '#000'],
        shadeClose: shadecolse,
        maxmin: true,
        area: [width + 'vw', height + 'vh']
    }
    options['btn'] = btnTxt;
    for (let i = 0; i < handers.length; i++) {
        options['btn' + (i + 1)] = handers[i];
    }

    return window.top.layer.open(options);
}
//btn

G.getLayerId = function (window) {
    let name = window.name;
    if (name && name.length > 18) {
        let id = name.slice(18);
        return id;
    }
    return "";
}

G.closeWindow = function (window) {
    let name = window.name;
    if (name && name.length > 18) {
        let id = name.slice(18);
        G.closeLayer(id);
    }
}


G.dateformatter = function (data) {
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    let d = date.getDate();
    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
G.datetimeformatter = function (date = new Date()) {
    if (!date) date = new Date();
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    let d = date.getDate();
    let hour = date.getHours();
    let miniute = date.getMinutes();
    let second = date.getSeconds();
    if (m < 10) m = "0" + m;
    if (d < 10) d = "0" + d;
    if (hour < 10) hour = "0" + hour;
    if (miniute < 10) miniute = "0" + miniute;
    if (second < 10) second = "0" + second;
    return `${y}-${m}-${d} ${hour}:${miniute}:${second}`;

}
G.dateparser = function (s) {
    if (!s) return new Date();
    let ss = (s.split('-'));
    let y = parseInt(ss[0], 10);
    let m = parseInt(ss[1], 10);
    let d = parseInt(ss[2], 10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
        return new Date(y, m - 1, d);
    } else {
        return new Date();
    }
}

G.datetimeparser = function (s) {
    if (!s) return new Date();
    return new Date(s);
}

G.fixHeight = function (percent, offset) {
    let toolHeight = document.getElementById("center-tools").clientHeight;
    let queryFormHeight = document.getElementById("queryForm").clientHeight;
    return (document.body.clientHeight - toolHeight - queryFormHeight) * 0.95;
}
G.fixHeightA = function (percent, offset, ...subdivs) {
    let totalHeight = document.body.clientHeight;
    if (subdivs && subdivs.length > 0) {
        for (let i = 0; i < subdivs.length; i++) {
            if (subdivs[i].indexOf("#") >= 0) subdivs[i] = subdivs[i].replaceAll("#","");
            let tempH = document.getElementById(subdivs[i]).clientHeight;
            totalHeight -= tempH;
        }
    }
    return totalHeight * percent - offset;
}
G.fixFooter = function (data) {
    if (!data || data.total <= 0 || data.rows.length <= 0) {
        let footer = $('.datagrid-pager')
        if (footer.length > 0) footer[0].style.display = "none";
    }
    else {
        let footer = $('.datagrid-pager')
        if (footer.length > 0) {
            footer[0].style.display = "";
        }
    }
}




G.getdateStr = function (date = new Date()) {
    let year = date.getFullYear();
    let month = date.getMonth() + 1; if (month < 10) month = "0" + month;
    let day = date.getDate(); if (day < 10) day = "0" + day;
    return `${year}-${month}-${day}`
}
G.getdatetimeStr = function (date = new Date()) {
    let year = date.getFullYear();
    let month = date.getMonth() + 1; if (month < 10) month = "0" + month;
    let day = date.getDate(); if (day < 10) day = "0" + day;
    let hour = date.getHours(); if (hour < 10) hour = "0" + hour;
    let minute = date.getMinutes(); if (minute < 10) minute = "0" + minute;
    let second = date.getSeconds(); if (second < 10) second = "0" + second;
    let millsecond = date.getMilliseconds();
    if (millsecond < 10) millsecond = "00" + millsecond;
    else if (millsecond < 100) millsecond = "0" + millsecond;
    return `${year}-${month}-${day} ${hour}:${minute}:${second}:${millsecond}`;
}


G.getSelectRows = function (tableId, singleSelect = true) {
    if (!tableId) return;
    if (tableId.indexOf("#") < 0) tableId = "#" + tableId;
    let rows = $(tableId).datagrid("getSelections");
    if (!rows || rows.length <= 0) {
        G.alert("请选择要处理的行");
        return;
    }
    if (rows.length > 1 && singleSelect) {
        G.alert("每次只能修改一行数据")
        return;
    }
    if (singleSelect) return rows[0];
    return rows;
}


G.setTokenAttribute = function (token, callback) {
    $.getJSON("/Home/SetTokenAttribute?token=" + token, function (data) {
        if (callback) callback(data);
    });
}

G.getFormObj = function (formId) {
    if (!formId) G.alert("请指定要提交的formId");
    if (formId.indexOf("#") < 0) formId = "#" + formId;
    $(formId).find(".easyui-checkbox").each(function (index, item) {
        if ($(item).checkbox('options').checked) {
            $(item).checkbox('setValue', true);
        }
    });
    let kv = $(formId).serializeArray();
    let obj = {};
    for (let i = 0; i < kv.length; i++) {
        if (kv[i]['value']) {
            obj[kv[i]['name']] = kv[i]['value']
        }
    }
    return obj;
}
G.getFormJson = function (formId) {
    let obj = G.getFormObj(formId);
    let json = JSON.stringify(obj)
    return json;
}

G.submitForm = function (formId, callback) {
    if (!formId) G.alert("请指定要提交的formId");
    if (formId.indexOf("#") < 0) formId = "#" + formId;
    $(formId).find(".easyui-checkbox").each(function (index, item) {
        if ($(item).checkbox('options').checked) {
            $(item).checkbox('setValue', true);
        }
    });
    let obj = $(formId).serialize();
    let url = $(formId).attr("action");
    let loading = G.loading("正在处理。。。")
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'JSON',
        caches: false,
        data: obj,

    })
        .done(function (data) {
            let msg = "操作成功";
            G.alert(msg);
            if (callback) callback(data);



        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            let msg = "操作失败";
            debugger
            if (jqXHR && jqXHR.responseText) {
                msg = jqXHR.responseText;
                if (jqXHR.responseJSON && jqXHR.responseJSON.Message) msg = jqXHR.responseJSON.Message;

            }


            G.alert(msg)
        })
        .always(() => {
            G.closeLayer(loading);
        })
}
G.sendData = function (url, data, callback) {
    let loading = G.loading("正在处理。。。")
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'JSON',
        data: data,

    })
        .done(function (data) {
            let msg = "操作成功";

            if (data.Status == 0 || data.IsSuccess == true) {
                if (data.Message) msg = data.Message;
                G.alert(msg);

                if (callback) callback(data);
            }


        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            let msg = "操作失败";

            if (jqXHR && jqXHR.responseText) {
                msg = jqXHR.responseText;
                if (jqXHR.responseJSON && jqXHR.responseJSON.Message) msg = jqXHR.responseJSON.Message;

            }


            G.alert(msg)
        })
        .always(() => {
            G.closeLayer(loading);
        })
}
G.getJSON = function (url, obj, callback, isasync = false) {
    if (isasync == false) $.ajaxSettings.async = false;  //设为同步请求
    $.getJSON(url,
        obj,
        function (data) {
            callback(data);
        });
    if (isasync == false)  $.ajaxSettings.async = true;  //设为异步请求
}


G.post = function (url, data, callback) {
    let loading = G.loading("正在处理。。。")
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'JSON',
        data: data,

    })
        .done(function (data) {
            if (callback) callback(data);
        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            let msg = "操作失败";
            if (jqXHR && jqXHR.responseText) {
                msg = jqXHR.responseText;
                if (jqXHR.responseJSON && jqXHR.responseJSON.Message) msg = jqXHR.responseJSON.Message;
            }
            G.alert(msg)
        })
        .always(() => {
            G.closeLayer(loading);
        })
}



if (!window.G) window.G = G;
//Object.freeze(window.G);

